package com.tanhua.server.server;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.db.BlackList;
import com.tanhua.model.db.Question;
import com.tanhua.model.db.Settings;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

@Service
public class UsersService {
    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private SettingsApi settingsApi;
    @DubboReference
    private QuestionApi questionApi;
    @DubboReference
    private BlackListApi blackListApi;
    public UserInfoVo findById(Long userId) {
        //1.查询数据
       UserInfo userInfo = userInfoApi.findById(userId);
       //2.将对象转化为vo,存值
        //vo对象和实体类属性大部分一致（用BeanUtils工具类，将参数一同名同属性，设置到参数二对象中）
        //不同得需要手动处理
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo,userInfoVo);
//        userInfoVo.setId(userId);
//        userInfoVo.setNickname(userInfo.getNickname());
        if(userInfo.getAge()!=null){
        userInfoVo.setAge(userInfo.getAge().toString());}
        //3.返回vo对象
        return userInfoVo;
    }

    public void update(UserInfo userInfo) {
        //更新头像和更新资料可以用一个
        userInfoApi.head(userInfo);
    }
   //设置通知开关
    public void saveSetting(Boolean likeNotification, Boolean pinglunNotification, Boolean gonggaoNotification) {
        Long userId = UserHolder.getUserId();

       Settings settings = settingsApi.findSetting(userId);
       if (settings==null){
           settings = new Settings();
           settings.setUserId(UserHolder.getUserId());
           settings.setLikeNotification(likeNotification);
           settings.setGonggaoNotification(gonggaoNotification);
           settings.setPinglunNotification(pinglunNotification);
           settingsApi.saveSetting(settings);
       }
       else {
          //更新不new对象
           settings.setLikeNotification(likeNotification);
           settings.setGonggaoNotification(gonggaoNotification);
           settings.setPinglunNotification(pinglunNotification);
           settingsApi.updateSetting(settings);
       }
    }
 // 查询通用设置
    public SettingsVo findSettings() {
        SettingsVo vo = new SettingsVo();
        Long userId = UserHolder.getUserId();
        //查问题
        Question question = questionApi.selectQuestion(userId);
        String txt= question==null?"你喜欢java么":question.getTxt();
        vo.setStrangerQuestion(txt);
        //查通知开关
        Settings settings = settingsApi.selectSetting(userId);
        if(settings!=null){
            vo.setPinglunNotification(settings.getPinglunNotification());
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setLikeNotification(settings.getLikeNotification());
        }
        //else不用写，vo有默认值
        String mobile = UserHolder.getUser().getMobile();
        vo.setPhone(mobile);
        vo.setId(userId);
        return vo;
    }

    public void saveQuestion(String content) {
        //查询当前用户的问题对象
            Question question =  questionApi.fingByUser(UserHolder.getUserId());
        //不存在就保存
        if(question==null){
            question = new Question();
            question.setTxt(content);
            question.setUserId(UserHolder.getUserId());
            questionApi.saveQuestion(question);
        }else { //存在就更新
            question.setTxt(content);
            questionApi.update(question);
        }


    }
 //分页查询黑名单
    public PageResult selectBlackList(Integer page ,Integer pageSize) {
        //获取用户id
        Long userId = UserHolder.getUserId();
        //根据用户id分页查询黑名单（Ipage《userInfo》）
        IPage<UserInfo> iPage =  blackListApi.selectBlackList(userId,page,pageSize);
        //讲ipage转化为页面所需数据(vo里有构造方法)
       return new PageResult(page,pageSize,iPage.getTotal(),iPage.getRecords());

    }
   //移除黑名单
    public void delBlack(Long blackId) {
        Long userId = UserHolder.getUserId();
        blackListApi.delBlack(blackId,userId);
    }
}
